package localSearch;

import instrumentation.CoverageBranchTarget;
import instrumentation.TrackerCoverage;

import java.util.Map;

import util.Filter;

import Operations.ValuesGenerator;

public class findFieldTarget {
	public static boolean canField1BeUsed(int BR,TrackerCoverage tracker) {
		Map<Integer,CoverageBranchTarget> trackers=tracker.getTrackers();
		CoverageBranchTarget condTarget = (CoverageBranchTarget) trackers.get(BR);

		return !condTarget.getfName_1().equals("");

	}
	public static boolean canField2BeUsed(int BR,TrackerCoverage tracker) {
		Map<Integer,CoverageBranchTarget> trackers=tracker.getTrackers();
		CoverageBranchTarget condTarget = (CoverageBranchTarget) trackers.get(BR);
		return !condTarget.getfName_2().equals("");
	}
	public static boolean canBothFieldsBeUsed(int BR,TrackerCoverage tracker) {
		Map<Integer,CoverageBranchTarget> trackers=tracker.getTrackers();
		CoverageBranchTarget condTarget = (CoverageBranchTarget) trackers.get(BR);
			return !condTarget.getfName_1().equals("") && !condTarget.getfName_2().equals("");
	}
	public static String detectField(int BR,TrackerCoverage tracker){	
		Map<Integer,CoverageBranchTarget> trackers=tracker.getTrackers();
		CoverageBranchTarget condTarget = (CoverageBranchTarget) trackers.get(BR);
//		if(condTarget.getfName_1().contains("param")||condTarget.getfName_2().contains("param"))
//				return "parameter";
//		else
		String tField=null;
			if(condTarget.getfName_1().length()!=0)
				tField= condTarget.getfName_1();
			else 
				if(condTarget.getfName_2().length()!=0)
					tField= condTarget.getfName_2();
				else
					if(condTarget.getArgsField().length()!=0)
							tField= "parameter";
						else
							return null;
			if(tField.contains("param"))
				return "parameter";
			else if(tField.contains("@this"))
				return null;
		return tField;
	}
	public static Class<?> getClassName(int BR,TrackerCoverage tracker){
		Map<Integer,CoverageBranchTarget> trackers=tracker.getTrackers();
		CoverageBranchTarget condTarget = (CoverageBranchTarget) trackers.get(BR);
		
		return condTarget.getcName();
		
	}
	public static String getPredicateValue(int BR,TrackerCoverage tracker){
		Map<Integer,CoverageBranchTarget> trackers=tracker.getTrackers();
		CoverageBranchTarget condTarget = (CoverageBranchTarget) trackers.get(BR);
		String predicateValue=null;
		//if((BR % 2)!=0)// true branch
		if(ValuesGenerator.getRandomInt(100)>50)
			if(!condTarget.getcValue_2().equals(""))
				predicateValue= condTarget.getcValue_2();
		
		if(predicateValue!=null && condTarget.getType()!=""){
			Class<?> pPermitiveTpe=Filter.getPermitiveClass(condTarget.getType());
			if( ValuesGenerator.getBoleean(.5f)&&pPermitiveTpe!=null){
				if(pPermitiveTpe== Integer.class) 
					return ""+((Integer)ValuesGenerator.mapValueToObject(pPermitiveTpe,predicateValue) + (ValuesGenerator.getBoleean(.5f) ? -1 : 1));
				if(pPermitiveTpe== Double.class) 
					return ""+((Double)ValuesGenerator.mapValueToObject(pPermitiveTpe,predicateValue) + (ValuesGenerator.getBoleean(.5f) ? -1 : 1));
				if(pPermitiveTpe== Float.class) 
					return ""+((Float)ValuesGenerator.mapValueToObject(pPermitiveTpe,predicateValue) + (ValuesGenerator.getBoleean(.5f) ? -1 : 1));
//				if(pPermitiveTpe== String.class) 
//					return ""+((Float)ValuesGenerator.mapValueToObject(pPermitiveTpe,predicateValue) + (ValuesGenerator.getBoleean(.5f) ? -1 : 1));
			
			}
			
		}
			
		return predicateValue;
	}
public static String getCallee(int BR,TrackerCoverage tracker){
	Map<Integer,CoverageBranchTarget> trackers=tracker.getTrackers();
	CoverageBranchTarget condTarget = (CoverageBranchTarget) trackers.get(BR);

	if(canBothFieldsBeUsed(BR,tracker)){
		if(ValuesGenerator.getBoleean(.5f))
			return condTarget.getCallee_1();
		else
			return condTarget.getCallee_2();
	}
	
	if(canField1BeUsed(BR,tracker))
		return condTarget.getCallee_1();

	if(canField2BeUsed(BR,tracker))
		return condTarget.getCallee_2();
	return null;
}

public static String getMethod(int BR,TrackerCoverage tracker){
	Map<Integer,CoverageBranchTarget> trackers=tracker.getTrackers();
	CoverageBranchTarget condTarget = (CoverageBranchTarget) trackers.get(BR);
		if(condTarget.getisMNamePublic()||condTarget.getmName().equals("<init>"))
			return condTarget.getmName();
	
	return null;
}
public static String getPrivateMethod(int BR,TrackerCoverage tracker){
	Map<Integer,CoverageBranchTarget> trackers=tracker.getTrackers();
	CoverageBranchTarget condTarget = (CoverageBranchTarget) trackers.get(BR);
		if(!condTarget.getisMNamePublic())
			return condTarget.getmName();
	
	return null;
}
  
private String getPredicateObjTpe(int BR,TrackerCoverage tracker){
	String objTpe=null;
	Map<Integer,CoverageBranchTarget> trackers=tracker.getTrackers();
	CoverageBranchTarget condTarget = (CoverageBranchTarget) trackers.get(BR);
	String f1=condTarget.getfName_1();
	if(f1.contains("@this:"))
		objTpe=f1.substring("@this:".length()).trim();
	
	String f2=f1=condTarget.getfName_2();
	if(objTpe==null)
	if(f2.contains("@this:"))
		objTpe=f2.substring("@this:".length()).trim();
	
	return objTpe;

}

public static Boolean isCallee1This(int BR,TrackerCoverage tracker){
	Map<Integer,CoverageBranchTarget> trackers=tracker.getTrackers();
	CoverageBranchTarget condTarget = (CoverageBranchTarget) trackers.get(BR);
	String callee1=condTarget.getCallee_1();
	if(callee1.contains("@this:"))
		return false;
return true;

	
}
}
